package com.hopu.product.actiom;

import java.util.List;
import java.util.Map;

import com.hopu.product.domain.Product;
import com.hopu.product.service.ProductService;
import com.hopu.product.service.impl.ProductServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends ActionSupport implements ModelDriven<Product> {

	private Product product = new Product();
	private ProductService service = new ProductServiceImpl();
	private int num;
	private String tagetUrl;
	
	private int[] ids;
	
	private List productList;
	
	public String findAll() {
		
		// 调用service的查全部方法
		productList = service.findAll();
		/*
		 * 1. 给Action添加属性和get/set方法
		 * 2.把数据存到request（Map）
		 * 3.把数据存到request(原生)
		 */
		
		Object num = ActionContext.getContext().getSession().get("messNum");
		Map map = (Map) ActionContext.getContext().get("request");
		map.put("messNum", num);
//		((Map)ActionContext.getContext().get("request")).put("messNum", num);
		ActionContext.getContext().getSession().remove("messNum");
		tagetUrl="page/findAll.jsp";
		return "ok";
	}
	
	public String findById(){
		
		product=service.findById(product);
		// 把数据存到request(Map)
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("p", product);
		tagetUrl = "page/findById.jsp";
		return "ok";
	}
	
	public String add(){
		num = service.add(product);
		ActionContext.getContext().getSession().put("messNum", num);
		return SUCCESS;
	}
	
	public String remove(){
		num = service.remove(product);
		ActionContext.getContext().getSession().put("messNum", num);
		
		return SUCCESS;// 转到查询全部的action
	}
	
	public String removes(){
		// 从值栈中得到提交的参数
//		String[] ids=(String[]) ActionContext.getContext().getParameters().get("ids");
		num = service.removes(ids);
		ActionContext.getContext().getSession().put("messNum", num);
		
		return SUCCESS;// 转到查询全部的action
	}
	
	public String updateFind(){
		product=service.findById(product);
		tagetUrl="page/update.jsp";
		
		return "ok";
	}
	
	public String update(){
		num = service.update(product);
		ActionContext.getContext().getSession().put("messNum", num);
		return SUCCESS;
	}
	
	
	
	
	@Override
	public Product getModel() {
		return product;
	}

	public List getProductList() {
		return productList;
	}

	public void setProductList(List productList) {
		this.productList = productList;
	}


	public String getTagetUrl() {
		return tagetUrl;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	
	
	

}
